<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields=array('id','title','image','price','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_goods_package',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'下架','1'=>'上架');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'套餐管理'),
			array('op'=>'edit','title'=>'添加套餐'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'套餐标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
	);
	$return['htmls']=$htmls;
	
	exi($return);
	}
	if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_goods_package',array('id'=>$id));
	$item['goodsids']=json_decode($item['goodsids'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('1'=>'上架','0'=>'下架');
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'套餐设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'套餐管理'),
			array('op'=>'edit','title'=>'添加套餐'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('type'=>'array','name'=>'goodsids','add_tip'=>'添加一个商品',
				'list'=>array(
					array('type'=>'string','title'=>'商品id','name'=>'title'),
					
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title'),
					array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'sale.get_good','choose_title'=>'选择商品'),
				)
				),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),	
			
			)
			);
	$return['htmls']=$htmls;
	exi($return);
	}
	if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','status','price','goodsids','image'),$postdata);
	$data['goodsids']=json_encode($data['goodsids']);
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_goods_package',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_goods_package',$data);
				exi('添加成功！','','list');
			}
			}
			if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_goods_package',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_goods_package',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>